package com.gmail.blackdog1987.carcare.adapter;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.activity.OrderCarChangeActivity;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshopDetail;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshopDetail;

public class OrderCarChangeAdapter extends BaseAdapter {

	List<TCarchangeshopDetail> datas;
	final OrderCarChangeActivity mContext;
	private boolean all;
	List<TCarchangeshopDetail> selected = new ArrayList<TCarchangeshopDetail>();

	public OrderCarChangeAdapter(OrderCarChangeActivity context, List<TCarchangeshopDetail> datas) {
		this.datas = datas;
		this.mContext = context;
	};
	
	public List<TCarchangeshopDetail> getSelected(){
		if(all){
			return datas;
		}else{
			return selected;
		}
	}
	
	public void addDatas(List<TCarchangeshopDetail> datas){
		this.datas.addAll(datas);
		notifyDataSetChanged();
	}
	
	public void setDatas(List<TCarchangeshopDetail> datas){
		if(this.datas==null){
			this.datas = new ArrayList<TCarchangeshopDetail>();
		}
		this.datas.clear();
		this.datas.addAll(datas);
		notifyDataSetChanged();
	}
	
	public void selectAll(){
		all = true;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {

		return datas==null?0:datas.size();
	}

	@Override
	public TCarchangeshopDetail getItem(int position) {
		return datas==null?null:datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final TCarchangeshopDetail m = getItem(position);
//		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order_meirong, null);
			holder = new ViewHolder();
			holder.ck = (CheckBox) convertView.findViewById(R.id.item_meirong_ck);
			holder.name = (TextView) convertView.findViewById(R.id.item_meirong_title);
			holder.info = (TextView) convertView.findViewById(R.id.item_meirong_info);
			holder.price = (TextView) convertView.findViewById(R.id.item_meirong_price);
//			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		final double price = m.getPrice();
		if(all){
			holder.ck.setChecked(true);
		}
		holder.ck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					mContext.addPrice(price);
					selected.add(m);
				}else{
					selected.remove(m);
					all = false;
					mContext.cancelAll();
					mContext.addPrice(-price);
				}
			}
		});
		holder.name.setText(m.getTitle());
		holder.info.setText(m.getInfo());
		holder.price.setText(String.valueOf(m.getPrice()));
//		convertView.setTag(holder);
		return convertView;
	}

	public class ViewHolder {
		CheckBox ck;
		TextView name;
		TextView info;
		TextView price;
	}

}
